﻿using System.Linq;
using System.Web.Mvc;
using DotNetDream.Models;
using System.Collections.Generic;

namespace DotNetDream.Controllers
{
    public class SlideController : Controller
    {
        /// <summary>
        /// Load the page
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Index()
        {
            //Load the sub
            SlideSubtitle sub = new SlideSubtitle("Content/MyWedding/Subtitle.xml");
            return View(sub.Subtitle);
        }

        /// <summary>
        /// Update the selected subtitle
        /// </summary>
        /// <param name="id"></param>
        /// <param name="text"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InsertUpdate(string id, string text, string start, string end)
        {
            var result = new { message = string.Empty };
            //Load current sub
            SlideSubtitle sub = new SlideSubtitle("Content/MyWedding/Subtitle.xml");
            //Insert new
            if (int.Parse(id) < 0)
            {
                sub.InsertItem(text, start, end);
                result = new { message = "Thêm dòng thành công !" };
            }
            //Update existing
            else
            {
                //Update
                sub.UpdateItem(id, text, start, end);
                result = new { message = "Chỉnh sửa thành công !" };
            }
            //Return result
            return Json(result);
        }

        /// <summary>
        /// Delete the selectes subtitle
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            //Load current sub
            SlideSubtitle sub = new SlideSubtitle("Content/MyWedding/Subtitle.xml");
            //Delete
            sub.DeleteItem(id);
            //Result
            return Json(new { message = "Xóa thành công !" });
        }
    }
}
